/**
 * @author liuxin
 * @e-mail lxtobe5@gmail.com
 * @update 2011.11.4
 */

package com.lx.utils
{
	import flash.utils.Dictionary;

	/**
	 * Class contains some methods deal with Key and Value. 
	 */	
	public class HashMap
	{
		private var _dict:Dictionary;
		
		/**
		 * Initialize the constructor,create an instance of Dictionary
		 */		
		public function HashMap()
		{
			_dict = new Dictionary(true);
		}
		
		/**
		 * @param key   put it into instance of Dictionary for Key
		 * @param value put it into instance of Dictionary for Value
		 */		
		public function add(key:*,value:*):void
		{
			_dict[key] = value;
		}
		
		/**
		 * @param key  delete value and key of instance of Dictionary by value
		 */		
		public function removeKey(key:*):void
		{
			var flag:Boolean = true;
			for (var _key:* in _dict)
			{
				if(_key === key)
				{
					delete _dict[_key];
					_key = null;
					flag = false;
					return;
				}
			}
			if(flag)
				throw new Error("Doesn't contain the key.");
		}
		
		/**
		 * @param value  delete value and key of instance of Dictionary by value
		 */		
		public function removeValue(value:*):void
		{
			var flag:Boolean = true;
			for (var _key:* in _dict)
			{
				if(_dict[_key] === value)
				{
					delete _dict[_key];
					_key = null;
					flag = false;
					return;
				}
			}
			if(flag)
				throw new Error("Doesn't contain the value.");
		}
		
		/**
		 * @param  value  search key from instance of Dictionary
		 * @return key
		 */		
		public function getKey(value:*):*
		{
			var flag:Boolean = true;
			for (var _key:* in _dict)
			{
				if(_dict[_key] === value)
				{
					flag = false;
					return _key;
				}
			}
			if(flag)
				throw new Error("Doesn't contain the type.");
		}
		
		/**
		 * @param  key  search value from instance of Dictionary
		 * @return value
		 */
		public function getValue(key:*):*
		{
			var flag:Boolean = true;
			for (var _key:* in _dict)
			{
				if(_key === key)
				{
					flag = false;
					return _dict[_key];
				}
			}
			if(flag)
				throw new Error("Doesn't contain the value.");
		}
		
		/**
		 * @return Array contains more keys
		 */		
		public function getKeys():Array
		{
			var a:Array = [];
			for (var key:* in _dict)
			{
				a.push(key);
			}
			return a;
		}
		
		/**
		 * @return Array contains more values
		 */		
		public function getValues():Array
		{
			var a:Array = [];
			for each (var value:* in _dict)
			{
				a.push(value);
			}
			return a;
		}
		
		/**
		 * @param  key      search from instance of Dictionary
		 * @return Boolean  if contains key,return true. else return false.
		 */		
		public function hasKey(key:*):Boolean
		{
			for (var _key:* in _dict)
			{
				if(_key === key)
					return true;
			}
			return false;
		}
		
		/**
		 * @param  value    search from instance of Dictionary
		 * @return Boolean  if contains value,return true. else return false.
		 */	
		public function hasValue(value:*):Boolean
		{
			for each (var _value:* in _dict)
			{
				if(_value === value)
					return true;
			}
			return false;
		}
	}
}